14 const int MAXC
= 30, MAXN
= 1000;
17 Classical 0-1 knapsack.
18 dp[i][j] = Using first i objects, maximum value that can be selected not exceeding weight j.
30 for (int i
=0; i
<n
; ++i
){
32 assert(1 <= v
[i
] && v
[i
] <= 100);
33 assert(1 <= w
[i
] && w
[i
] <= 30);
36 for (int j
=0; j
<=MAXC
; ++j
){
37 dp
[0][j
] = (j
< w
[0] ? 0 : v
[0]);
40 for (int i
=1; i
<n
; ++i
){
42 for (int j
=1; j
<=MAXC
; ++j
){
43 dp
[i
][j
] = dp
[i
-1][j
];
45 dp
[i
][j
] = max(dp
[i
][j
], dp
[i
-1][j
-w
[i
]] + v
[i
]);
54 assert(1 <= c
&& c
<= 30);
58 cout
<< answer
<< endl
;